'use strict';

angular.module('app')
    .controller('updateController', ['$scope', '$rootScope', '$state', '$timeout',
        function($scope, $rootScope, $state, $timeout) {
            var title = "";
            if($state.includes('**.reptileinfo.update')){
                title="编辑爬虫参数信息";
                var configId = $state.params.id;
                activate(configId);
                validate();
                $scope.isDisabled = false;
            }else if($state.includes('**.reptileinfo.create')){
                title="添加爬虫参数信息";
                validate();
            }
            
            $scope.title = $rootScope.title = title;
            $scope.loading = true;
            function validate() {
                jQuery("#reptileform").validate({
                    rules: {
                        platNo: {
                            required: true
                        },
                        type: {
                            required: true
                        },
                        site: {
                            required: true
                        }
                    },
                    messages: {
                        platNo: {
                            required: '平台不能为空'
                        },
                        type: {
                            required: '类型不能为空'
                        },
                        site:{
                            required: '网站地址不能为空'
                        }
                    },
                    submitHandler: function () {
                        $scope.submit();
                    }
                });
            }
            
            //初始化验证
            $scope.submit= function(){
                $scope.loading = true;
                
                var type = $scope.record.type;
                var keyword = $scope.record.keyword;
                if ((type == 1 || type == 2)) {
                	if (keyword == null || keyword == '') {
                		$scope.alertModal({"content":"平台类型为网站-文章/微信订阅号，请输入关键字"});
                    	return;
                	}
                } else {
                	$scope.record.keyword = "";
                }
                
                var m = $scope.record;
                console.log(m);
                if(m){
                    $scope.isDisabled = true;//提交disabled
                    $.ajax({
                        url : $scope.record.configId ? $scope.app.project_name+'/hconfig/update' : $scope.app.project_name+'/hconfig/add',
                        data: m
                    }).then(callback);
                }
                function callback(result){
                    if(result.httpCode ==200){
                        $scope.alertModal({"content":"操作成功"});
                        $timeout(function(){
                            $state.go('main.plat.reptileinfo.list');
                        },1000);
                    }else{
                        $scope.alertModal({"content":"操作失败"});
                        $scope.isDisabled = false;
                    }
                    $scope.loading = false;
                }
            };

            // 初始化页面
            function activate(configId) {
                $scope.loading = true;
                $.ajax({
                    url : $scope.app.project_name+'/hconfig/read/detail',
                    data: {'configId': configId}
                }).then(function(result) {
                    $scope.loading = false;
                    if (result.httpCode == 200) {
                        $scope.record = result.data;
                        $scope.record.configId=configId;
                        console.log( result.data);
                    } else {
                        $scope.msg = result.msg;
                    }
                    $scope.$apply();
                });
            }
            
            $scope.getplatselect=function () {
                $.ajax({
                    url : $scope.app.project_name+'/platInfoManage/platInfo/getPlatselect'
                }).then(function(result) {
                    $scope.loading = false;
                    if (result.httpCode == 200) {
                        $scope.platList = result.data;
                    } else {
                        $scope.msg = result.msg;
                    }
                    $scope.$apply();
                });
            }
            $scope.getplatselect();
            
            $scope.changeType = function (){
    			var type = $scope.record.type;
                if (type == 3) {
                	$("#type_keyword").hide();
                } else {
                	$("#type_keyword").show();
                }
    		}

        }]);